/**
 * @description 所有全局配置的状态管理，如无必要请勿修改
 */
import { SettingsModuleType } from '/#/store'
import { isJson } from '@/utils/validate'
import { suiteName } from '@/config'

const prekey = suiteName
const getLocalStorage = (key: string) => {
  const value: string | null = localStorage.getItem(prekey + '' + key)
  return value && isJson(value) ? JSON.parse(value) : false
}

const setLocalStorage = (key: string, value: any) => {
  if (value instanceof String || Number) {
    localStorage.setItem(prekey + '' + key, value)
  } else {
    localStorage.setItem(prekey + '' + key, JSON.stringify(value))
  }
}

const corpId = getLocalStorage('corpId')

export const useSettingsStore = defineStore('settings', {
  state: (): SettingsModuleType => ({
    corpId,
  }),
  getters: {
    getCorpId: (state) => state.corpId,
  },
  actions: {
    setCorpId(corpId: string) {
      this.corpId = corpId
      setLocalStorage('corpId', this.corpId)
    },
  },
})
